package com.dayuanit.dy8.mall.springbootmall.service.impl;

import com.dayuanit.dy8.mall.springbootmall.dto.CommonDTO;
import com.dayuanit.dy8.mall.springbootmall.entity.Commodity;
import com.dayuanit.dy8.mall.springbootmall.entity.CommodityWithBLOBs;
import com.dayuanit.dy8.mall.springbootmall.mapper.CommodityMapper;
import com.dayuanit.dy8.mall.springbootmall.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {


    @Value("${image.url}")
    private String imageUrl;

    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public CommodityWithBLOBs loadCommon(int commonId) {
        final CommodityWithBLOBs commodityWithBLOBs = commodityMapper.selectByPrimaryKey(commonId);
        commodityWithBLOBs.setPictureUrl(imageUrl + commodityWithBLOBs.getPictureUrl());
        return commodityWithBLOBs;
    }

    @Override
    public List<CommonDTO> loadBuyCommon(int commonId, int buyNum) {

        final Commodity commodity = commodityMapper.getCommodity(commonId);
        final CommonDTO commonDTO = new CommonDTO();
        final List<CommonDTO> list = new ArrayList<>();
        list.add(commonDTO);

        commonDTO.setAmount(commodity.getPrice() * buyNum);
        commonDTO.setBuyNum(buyNum);
        commonDTO.setCommoId(commonId);
        commonDTO.setCommoName(commodity.getCommoName());
        commonDTO.setPicUrl(imageUrl + commodity.getPictureUrl());
        commonDTO.setPrice(commodity.getPrice());
        return list;
    }
}
